// mircoApp/meituan_refund/search/search.js
const skinBehavior = require('../../../utils/themeSkin/skinBehavior');
const app = getApp();
Page({

  behaviors: [skinBehavior],
  /**
   * 页面的初始数据
   */
  data: {
    is_first:true,
    search_type:"keyword",
    show_tips:false,
    value_category:1,
    searchId:"",
    globalData: {
      navHeight: 0,
      navTop: 0,
      jnheight: 0,
      jnwidth: 0,
    },
    keyword: "",

    list: [],
    page: 1,
    last_page: 1,

    sId: "",
    lng: "",
    lat: "",
    cityId: "",
    cate_id: "",
    options:{}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    
    let address_point = wx.getStorageSync("address-point");
    let language = wx.getStorageSync('langIndex');
    this.setData({
      'language': language.en,
      value_category:options.category,
      options
    });
    if(address_point){
      this.data.lng = address_point.point.lng;
      this.data.lat = address_point.point.lat;
      this.getData()
    }else {
      this.getLocationInfo();
    }
    this.getCategory();
  
    // this.initNavBar();
  },

  show_popup(){
    this.setData({show_tips:!this.data.show_tips})
  },
  choose(e){
    let value_category = e.currentTarget.dataset.num;
    this.setData({value_category,show_tips:false})
    if(value_category==-1){
      this.data.search_type = 'keyword'
    }else{
      this.data.search_type = 'id';
    }
  },
  getLocationInfo() {
    try {
      this._getLocation((mapdata, e) => { //获取定位的城市，拿到定位的信息
        console.log(mapdata, "mapdata====")
        this.data.point.lng = e.lng;
        this.data.point.lat = e.lat;
        this.getData();
      });
    } catch (e) {
      this.getData();
    }
  },
  goto_detail(e) {
    let data = e.currentTarget.dataset.item;
    let ids = this.data.search_type=='keyword'?data.ids:this.data.options.ids;
    if(this.data.options.ids==153){
      ids = 153;
    }
    wx.navigateTo({
      url: `/mircoApp/meituan_refund/detail/detail?id=${data.couponPackDetail.skuViewId}&ids=${ids}`
    })
  },
  getData(page = 1) {
    let json = {
      page,
    }
    json.keyword = this.data.keyword||'';
    json.search_type = this.data.search_type;
    if (this.data.point) {
      json.latitude = this.data.point.lat;
      json.longitude = this.data.point.lng;
    }
    if(this.data.searchId){
      json.searchId = this.data.searchId;
    }
    if(this.data.options.ids!==-1){
      json.ids = this.data.options.ids;
    }

    app._postNetWork({
      url: app.getNetAddresss('plugin.meituan-group-buy.frontend.good.good'),
      data: json,
      success: resdata => {
        let {
          result,
          data
        } = resdata.data;
        if (result) {
          if (page == 1) {
            this.setData({
              list: data.data
            })
          } else {
            this.data.list = this.data.list.concat(data.data)
            this.setData({
              list: this.data.list
            })
          }
          this.data.hasNext = data.hasNext;
          this.data.page+=1;
          this.data.searchId = data.searchId||'';
        } 
      }
    })
  },
  InputHandle(e) {
    this.setData({
      keyword: e.detail.value
    });
  },
  initNavBar() {
    //获取菜单按钮的布局位置信息
    let menuButtonObject = wx.getMenuButtonBoundingClientRect();
    //获取系统信息
    wx.getSystemInfo({
      success: res => {
        //状态栏的高度
        let statusBarHeight = res.statusBarHeight,
          //胶囊按钮与顶部的距离
          navTop = menuButtonObject.top,
          navHeight = statusBarHeight + menuButtonObject.height + (menuButtonObject.top - statusBarHeight) * 2;
        let globalData = {};
        globalData.navHeight = navHeight; //导航栏高度
        globalData.navTop = navTop; //胶囊按钮与顶部的距离
        globalData.jnheight = menuButtonObject.height; //胶囊的高度
        globalData.jnwidth = menuButtonObject.width; //胶囊的宽度
        this.setData({
          globalData
        })
      },
      fail(err) {
        console.log(err);
      }
    });
  },
  navBack: function () {
    wx.navigateBack()
  },
  getCategory() {
    app._getNetWork({
      url: app.getNetAddresss('plugin.meituan-group-buy.frontend.good.category'),
      success: resdata => {
        let {
          result,
          msg,
          data
        } = resdata.data;
        if (result !== 1) {
          return app.tips(msg);
        }
        this.setData({category_list: data});
      }
    })
  },
  search() {
    let ids = '';
    if(this.data.value_category==1){
      ids = this.data.category_list[0].ids.join('');
    }else if(this.data.value_category==2){
      ids = '203';
    }else if(this.data.value_category==-1){
      ids = -1;
    }else{
      ids = this.data.category_list[this.data.category_list.length-1].ids.join('');
    }
    this.data.searchId = '';
    this.data.is_first = true;
    if(this.data.value_category==-1){
      this.data.search_type = "keyword";
    }else{
      this.data.search_type = "id";
    }
    this.data.options.ids = ids;
    this.getData();
  },
  navHome: function () {
    wx.navigateTo({
      url: '/packageI/citypickerPage/citypickerPage?plu=meituan&path=search'
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    // this.getData();
    this.data.page=1;
    this.data.search_type = 'keyword';
    this.data.is_first=true;
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    if (!this.data.hasNext) {
      return;
    }
    let page = this.data.page;
    this.getData(page);
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})